<?php

namespace App\Console\Commands\Elasticsearch;

use App\Models\Product;
use Illuminate\Console\Command;

class SyncProducts extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'es:sync-products';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '将商品数据同步到elasticsearch';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //获取elasticsearch对象
        $es = app("es");
        Product::query()
        //预加载sku和商品属性数据，避免n+1问题
        ->with(['skus',"properties"])
        //使用chunkById避免一次性加载过多数据
        ->chunkById(100,function($products)use($es){
            $this->info(sprintf("正在同步id范围为 %s 至 %s的商品",$products->first()->id,$products->last()->id));
            //初始化请求题
            $req = ["body" => []];
            //遍历商品
            foreach($products as $product){
                $data = $product->toESArray();
                $req['body'][] = [
                    "index" => [
                        "_index" => "products",
                        "_id" => $data['id'],
                    ],
                ];
                $req['body'][] = $data;
            }
            try{
                //使用bulk方法批量创建
                $es->bulk($req);
            }catch(\Exception $e){
                $this->error($e->getMessage());
            }
        });
      
        $this->info("同步成功");
    }
}
